const mongoose = require('mongoose');

const hostname = "0.0.0.0";
const port = 27017;//mongoDB专用端口
const dbname = "uni";//库名

const username = "";
const password = "";
const options = {//如不写,会报警告warming
    useNewUrlParser: true,
	useUnifiedTopology: true,
	useFindAndModify: false
}

/**
 * mongoose.connect('mongodb://用户名:密码@127.0.0.1:27017/数据库名称');//格式
 * 连接多个数据库，只需要设置多个url以,隔开，同时设置mongos为true
 * 
 * const DB_URL = `mongodb://${username}:${password}@${hostname}:${port}/${dbname}`;
 * 上线时若没有用户名和密码,但是又写了就可能链接失败,索性省略不写
 */

const DB_URL = `mongodb://${hostname}:${port}/${dbname}`;
mongoose.connect(DB_URL, options, (err)=>{
	if(!err){
		console.log("mongodb 连接成功 (^し^)");
	}
	else{// 不要throw
		console.log("连接失败", err)
	}
});

/**
 * TODO: 监听: 可写可不写,没什么用,了解
 * const connection = mongoose.connection;
 * connection.on("connected", () => {// 连接成功}) 
 * connection.on("open", () => {// 数据库开启});
 * connection.on('error', err => {// 链接异常})
 * connection.on('disconnected', () => {// 监听断开链接})
 * mongoose.disconnect(function(){// 主动断开操作})
 */

module.exports = mongoose;